<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta  charset="UTF-8">
    <script type="text/javascript" charset="UTF-8" src="/js/easyui/jquery.easyui.min.js" th:src="@{js/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript" charset="UTF-8" src="/js/easyui/locale/easyui-lang-zh_CN.js" th:src="@{js/easyui/locale/easyui-lang-zh_CN.js}"></script>
</head>
<body>
	<table id ="permissionDataGrid"></table>
    <form id ="permissionDialog"></form>
</body>

<script type="text/javascript" >
    $(function(){
        permission = {
            //添加记录
            add : function(){
                $('#permissionDialog').dialog('open').dialog('setTitle','添加功能');
                $('#permissionDialog').dialog('refresh','/permission/add.ftl');
            },
            //删除记录
            remove : function(){
                var rows = $('#permissionDataGrid').datagrid('getSelections');
                if(rows.length > 0){
                    $.messager.confirm('确定操作','您正的要删除所选的记录吗？',function(flag){
                        if (flag){
                            var ids = [];
                            for (var i = 0; i < rows.length; i ++) {
                                ids.push(rows[i].id);
                            }

                            $.ajax({
                                type : 'delete',
                                url : '/permissions/'+rows[0].id,
                                data : {
                                    ids : ids.join(','),
                                },
                                beforeSend : function () {
                                    $('#permissionDataGrid').datagrid('loading');
                                },
                                success : function (data) {
                                    if (data) {
                                        $('#permissionDataGrid').datagrid('loaded');
                                        $('#permissionDataGrid').datagrid('load');
                                        $('#permissionDataGrid').datagrid('unselectAll');
                                        $.messager.show({
                                            title : '提示',
                                            msg : data + '个功能被删除成功！',
                                        });
                                    }
                                },
                            });
                        }
                    });
                } else {
                    $.messager.alert('警告','请选择要删除的记录！','info');
                }
            },
            edit : function(){
                var rows = $('#permissionDataGrid').datagrid('getSelections');
                if (rows.length > 1){
                    $.messager.alert('警告操作','编辑记录只能选定一条数据！','warning');
                } else if (rows.length == 1){
                    $.ajax({
                        type : 'GET',
                        url : '/permissions/'+rows[0].id,
                        beforeSend : function(){
                            $.messager.progress({
                                text : '正在尝试获取数据...',
                            });
                        },
                        success : function(data){
                            $.messager.progress('close');
                            if(data){
                                console.log(data);
                                $('#permissionDialog').load('/permission/update.ftl');
                                //$('#permissionDialog').dialog('refresh','/permission/update.ftl');
                                $('#permissionDialog').form('load',{
                                    id : data.id,
                                    text : data.text,
                                    pid : data.pid,
                                    iconCls : data.iconCls,
                                    url : data.url,
                                }).dialog('open').dialog('setTitle','修改功能').dialog('center');

                                $('#pid').combotree({
                                    url : 'menu.php',
                                    required : true,
                                    lines : true,
                                    //multiple : true,
                                    //checkbox : true,
                                    //onlyLeafCheck : true,
                                    onLoadSuccess : function (employee, data) {
                                        var _this = this;
                                        if (data) {
                                            $(data).each(function (index, value) {
                                                if ($.inArray(value.text, pid) != -1) {
                                                    $(_this).tree('check', value.target);
                                                }
                                                if (this.state == 'closed') {
                                                    $(_this).tree('expandAll');
                                                }
                                            });
                                        }
                                    },
                                });
                            } else {
                                $.messager.alert('获取失败','未知错误导致失败，请重试！','warning');
                            }
                        }
                    })
                } else if (rows.length == 0){
                    $.messager.alert('警告操作！','编辑记录至少选定一条数据！');
                }
            },
            search : function(){

            }
        }
        $('#permissionDataGrid').datagrid({
            iconCls : 'icon-search',//标题图标
            title : '功能列表',//标题名称
            fit : true,//全屏
            border : false,//无边框
            fitColumns : true,//列全屏
            striped : true,//显示斑马线效果
            rownumbers : true,//显示一个行号列
            pagination : true,//分页
            pageSize : 10,//每页显示10条
            pageList : [10,20,30,40,50],//每页条数可选
            pageNumber : 1,//默认显示第1页
            sortName : 'id',//按X列排序
            sortOrder : 'desc',//排序类型
            //工具栏
            toolbar : [
                {
                    iconCls:'icon-add',
                    text:'增加',
                    handler:function(){
                        permission.add();
                    }
                },'-',{
                    iconCls:'icon-edit',
                    text:'修改',
                    handler:function(){
                        permission.edit();
                    }

                },'-',{
                    iconCls:'icon-save',
                    text:'删除',
                    handler:function(){
                        permission.remove();
                    }
                }
            ],
            //内容表格
            columns : [[
                {field:'id',title:'id',checkbox:'true',align:'center',width:100,sortable:true,},
                {field:'name',title:'功能名称',align:'center',width:100,sortable:true,},
                {field:'url',title:'URL',align:'center',width:100,sortable:true,},
                {field:'iconCls',title:'图标',align:'center',width:100,sortable:true,},
                {field:'openMode',title:'打开方式',align:'center',width:100,sortable:true,},
                {field:'status',title:'状态',align:'center',width:100,sortable:true,
                    formatter:function(value,row,index){
                        if(value){
                            return '启用';
                        }else{
                            return '禁用';
                        }
                    }
                },
                {field:'created_on',title:'创建时间',align:'center',width:100,sortable:true,},
            ]],
            url : '/permissions',
            method : 'GET',
        });

        $('#permissionDialog').dialog({
            width : 350,
            modal : true,
            closed : true,
            //iconCls : 'icon-add',
            buttons : [{
                text : '提交',
                iconCls : 'icon-add-new',
                handler : function () {
                    var permission ={"name":$('#name').val(),"url":$('#url').val(),"icon":$('#icon').val()};
                    if ($('#permission').form('validate')) {
                        $.ajax({
                            url : '/permissions',
                            type : 'POST',
                            contentType : 'application/json',
                            dataType : 'json',
                            data : JSON.stringify(permission),
                            beforeSend : function () {
                                $.messager.progress({
                                    text : '正在添加功能中......',
                                });
                            },
                            success : function (data,response,status) {
                                $.messager.progress('close');
                                if (data > 0) {
                                    $.messager.show({
                                        title : '提示',
                                        msg : '员工成功！',
                                    });
                                    $('#permissionDialog').dialog('close');
                                    $('#permissionDataGrid').datagrid('loaded');
                                } else {
                                    $.messager.alert('失败！','未知错误导致失败，请重试！','warning');
                                }
                            }
                        });
                    }

                },
            },{
                text : '取消',
                iconCls : 'icon-redo',
                handler : function (){
                    $('#permission').dialog('close').form('reset');
                },
            }],
        });

    });
</script>

</html>